#include "disciplinewizard.h"

#include <QDomDocument>

DisciplineWizard::DisciplineWizard(QWidget *parent) :
  QWizard(parent),
  ui(new Ui::DisciplineWizard)
{
  ui->setupUi(this);

  for (int i=0;i<15;++i)
  {
    _circlePages.push_back(new CircleWizardPage(i+1));
    addPage(_circlePages.last());
  }

  connect(ui->addArt,SIGNAL(clicked()),this,SLOT(addArt()));
  connect(ui->removeArt,SIGNAL(clicked()),this,SLOT(removeArt()));
}

DisciplineWizard::~DisciplineWizard()
{
  delete ui;
}

void DisciplineWizard::addArt()
{
  QListWidgetItem* item = new QListWidgetItem("Art");
  item->setFlags(item->flags() | Qt::ItemIsEditable);
  ui->art->addItem(item);
}

void DisciplineWizard::removeArt()
{
  if (ui->art->selectedItems().size() > 0) delete ui->art->selectedItems().first();
}

QList<QListWidgetItem*> listItems(QListWidget* list)
{
  QList<QListWidgetItem*> result;
  for(int i=0;i<list->count();++i)
    result << list->item(i);
  return result;
}

QString DisciplineWizard::result()
{
  QDomDocument doc;
  QDomElement root = doc.createElement("discipline");
  root.setAttribute("name",ui->name->text());

  // Attributs principaux
  QDomElement attributes = doc.createElement("attributes");
  foreach(QListWidgetItem* item, listItems(ui->attributes))
  {
    if (item->checkState() != Qt::Checked) continue;
    QDomElement attr = doc.createElement("attribute");
    attr.setAttribute("id",item->text());
    attributes.appendChild(attr);
  }
  root.appendChild(attributes);

  // Restrictions raciales
  QDomElement races = doc.createElement("restrictions");
  foreach(QListWidgetItem* item, listItems(ui->restrictions))
  {
    if (item->checkState() != Qt::Checked) continue;
    QDomElement attr = doc.createElement("race");
    attr.setAttribute("name",item->text());
    races.appendChild(attr);
  }
  root.appendChild(races);

  // Compétences d'art
  QDomElement arts = doc.createElement("arts");
  foreach(QListWidgetItem* item, listItems(ui->art))
  {
    QDomElement attr = doc.createElement("art");
    attr.setAttribute("name",item->text());
    arts.appendChild(attr);
  }
  root.appendChild(arts);

  // Endurance
  QDomElement endurance = doc.createElement("endurance");
  endurance.setAttribute("death",ui->endu1->text());
  endurance.setAttribute("unconcious",ui->endu2->text());
  root.appendChild(endurance);

  // Circles
  QDomElement circles = doc.createElement("circles");
  int c=1;
  foreach(CircleWizardPage* p, _circlePages)
  {
    QDomElement circle = doc.createElement("circle");
    circle.setAttribute("n",c++);

    QDomElement talents = doc.createElement("talents");
    foreach(QListWidgetItem* item, listItems(p->ui->talents))
    {
      QDomElement talent = doc.createElement("talent");
      talent.setAttribute("name",item->text());
      talent.setAttribute("discipline",(item->checkState() == Qt::Checked ? "true" : "false"));
      talents.appendChild(talent);
    }
    circle.appendChild(talents);

    if (listItems(p->ui->karmaUsage).size() > 0)
    {
      QDomElement karmas = doc.createElement("karmas");
      foreach(QListWidgetItem* item, listItems(p->ui->karmaUsage))
      {
        QDomElement karma = doc.createElement("karma");
        karma.setAttribute("usage",item->text());
        karmas.appendChild(karma);
      }
      circle.appendChild(karmas);
    }

    QDomElement bonuses = doc.createElement("bonuses");
    if (!p->ui->physDefBonus->text().isEmpty())
    {
      QDomElement bonus = doc.createElement("bonus");
      bonus.setAttribute("type","DP");
      bonus.setAttribute("value",p->ui->physDefBonus->text());
      bonuses.appendChild(bonus);
    }
    if (!p->ui->magDefBonus->text().isEmpty())
    {
      QDomElement bonus = doc.createElement("bonus");
      bonus.setAttribute("type","DM");
      bonus.setAttribute("value",p->ui->magDefBonus->text());
      bonuses.appendChild(bonus);
    }
    if (!p->ui->socialDefBonus->text().isEmpty())
    {
      QDomElement bonus = doc.createElement("bonus");
      bonus.setAttribute("type","DS");
      bonus.setAttribute("value",p->ui->socialDefBonus->text());
      bonuses.appendChild(bonus);
    }
    if (!p->ui->karmaBonus->text().isEmpty())
    {
      QDomElement bonus = doc.createElement("bonus");
      bonus.setAttribute("type","karma");
      bonus.setAttribute("value",p->ui->karmaBonus->text());
      bonuses.appendChild(bonus);
    }
    if (!p->ui->recupBonus->text().isEmpty())
    {
      QDomElement bonus = doc.createElement("bonus");
      bonus.setAttribute("type","recup");
      bonus.setAttribute("value",p->ui->recupBonus->text());
      bonuses.appendChild(bonus);
    }
    if (!p->ui->initBonus->text().isEmpty())
    {
      QDomElement bonus = doc.createElement("bonus");
      bonus.setAttribute("type","initiative");
      bonus.setAttribute("value",p->ui->initBonus->text());
      bonuses.appendChild(bonus);
    }
    circle.appendChild(bonuses);

    circles.appendChild(circle);
  }
  root.appendChild(circles);

  // Result
  doc.appendChild(root);
  return doc.toString();
}
